<?php

class SystemController extends Uuch_BaseController{
    
    public function init() {
        parent::init();
        $this->_helper->layout->setLayout('admin');
    }
    
    public function indexAction(){
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
        $this->view->city_list = $db->fetchAll(
			"SELECT id,`name` FROM uuch_city"
		);
    }
	
	public function cityQueryAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('name') != '') {
			$where .= $db->quoteInto(" AND m.`name` LIKE ?", '%' . $this->_getParam('name') . '%');
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$count = $db->fetchOne("SELECT COUNT(*) FROM uuch_city m WHERE $where");
		$rows = $db->fetchAll("SELECT m.*,IFNULL(s.`name`, '&nbsp;') parentName FROM uuch_city m LEFT JOIN uuch_city s ON m.parentId = s.id WHERE $where ORDER BY m.sortid LIMIT $pageSize");
		echo '{"count":"' . $count . '","items":' . json_encode($rows) . '}';
		exit;
	}
	
	public function cityPagerAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('name') != '') {
			$where .= $db->quoteInto(' AND m.`name` LIKE ?', '%' . $this->_getParam('name') . '%');
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$pageIndex = $this->_getParam('pageindex') > 0 ? $this->_getParam('pageindex') : 1;
		$offset = ($pageIndex - 1) * $pageSize;
		$rows = $db->fetchAll("SELECT m.*,s.`name` parentName FROM uuch_city m LEFT JOIN uuch_city s ON m.parentId = s.id WHERE $where ORDER BY m.sortid LIMIT $offset, $pageSize");
		echo json_encode($rows);
		exit;
	}
	
	public function cityDetailsAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$result = $db->fetchRow(
			"SELECT *,`default` cityDefault FROM uuch_city WHERE id = :id",
			array('id' => $this->_getParam('id'))
		);
		echo json_encode($result);
		exit;
	}
	
	public function cityEditAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
        $set = array (
			'name' => $this->_getParam('cityName'),
			'parentId' => (int)$this->_getParam('cityParentId'),
			'type' => $this->_getParam('cityType'),
			'ename' => $this->_getParam('cityEname'),
			'serviceFee' => $this->_getParam('cityServiceFee'),
			'sortid' => $this->_getParam('citySortid'),
			'percent' => $this->_getParam('cityPercent'),
			'state' => $this->_getParam('cityState'),
			'default' => $this->_getParam('cityDefault'),
		);
		$where = $db->quoteInto('id = ?', $this->_getParam('id'));
		$rows_affected = $db->update('uuch_city', $set, $where);
		echo 'editok';
		exit;
	}
	
	public function cityAddAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$row = array (
			'name' => $this->_getParam('cityName'),
			'parentId' => (int)$this->_getParam('cityParentId'),
			'type' => $this->_getParam('cityType'),
			'ename' => $this->_getParam('cityEname'),
			'serviceFee' => $this->_getParam('cityServiceFee'),
			'sortid' => $this->_getParam('citySortid'),
			'percent' => $this->_getParam('cityPercent'),
			'state' => $this->_getParam('cityState'),
			'default' => $this->_getParam('cityDefault'),
		);
		$rows_affected = $db->insert('uuch_city', $row);
		echo 'addok';
		exit;
	}
	
	public function cityDelAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->delete('uuch_city', $where);
		echo json_encode(true);
		exit;
	}
	
	public function cityEnableAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_city', array('state' => 1), $where);
		echo json_encode(true);
		exit;
	}
	
	public function cityDisableAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_city', array('state' => 0), $where);
		echo json_encode(true);
		exit;
	}
	
	public function cityDefaultAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_city', array('default' => 1), $where);
		echo json_encode(true);
		exit;
	}
	
	public function distributionAction(){
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
        $this->view->city_list = $db->fetchAll(
			"SELECT id,`name` FROM uuch_city"
		);
		$this->view->shiptype_list = $db->fetchAll(
			"SELECT id,`name` FROM uuch_shiptype"
		);
		$this->view->agent_list = $db->fetchAll(
			"SELECT id,`name` FROM uuch_agentcompany"
		);
    }
	
	public function distributionQueryAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('typeName') != '') {
			$where .= $db->quoteInto(" AND t.`name` LIKE ?", '%' . $this->_getParam('typeName') . '%');
		}
		if ($this->_getParam('cityName') != '') {
			$where .= $db->quoteInto(" AND c.`name` LIKE ?", '%' . $this->_getParam('cityName') . '%');
		}
		if ($this->_getParam('agentName') != '') {
			$where .= $db->quoteInto(" AND a.`name` LIKE ?", '%' . $this->_getParam('agentName') . '%');
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$count = $db->fetchOne("SELECT COUNT(*) FROM uuch_shipFeeConfig s LEFT JOIN uuch_city c ON s.cityid = c.id LEFT JOIN uuch_agentcompany a ON s.agentCompanyId = a.id LEFT JOIN uuch_shiptype t ON s.shipTypeId = t.id WHERE $where");
		$rows = $db->fetchAll("SELECT s.*,c.`name` cityName,a.`name` agentName,t.`name` typeName FROM uuch_shipFeeConfig s LEFT JOIN uuch_city c ON s.cityid = c.id LEFT JOIN uuch_agentcompany a ON s.agentCompanyId = a.id LEFT JOIN uuch_shiptype t ON s.shipTypeId = t.id WHERE $where LIMIT $pageSize");
		echo '{"count":"' . $count . '","items":' . json_encode($rows) . '}';
		exit;
	}
	
	public function distributionPagerAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('typeName') != '') {
			$where .= $db->quoteInto(" AND t.`name` LIKE ?", '%' . $this->_getParam('typeName') . '%');
		}
		if ($this->_getParam('cityName') != '') {
			$where .= $db->quoteInto(" AND c.`name` LIKE ?", '%' . $this->_getParam('cityName') . '%');
		}
		if ($this->_getParam('agentName') != '') {
			$where .= $db->quoteInto(" AND a.`name` LIKE ?", '%' . $this->_getParam('agentName') . '%');
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$pageIndex = $this->_getParam('pageindex') > 0 ? $this->_getParam('pageindex') : 1;
		$offset = ($pageIndex - 1) * $pageSize;
		$rows = $db->fetchAll("SELECT s.*,c.`name` cityName,a.`name` agentName,t.`name` typeName FROM uuch_shipFeeConfig s LEFT JOIN uuch_city c ON s.cityid = c.id LEFT JOIN uuch_agentcompany a ON s.agentCompanyId = a.id LEFT JOIN uuch_shiptype t ON s.shipTypeId = t.id WHERE $where LIMIT $offset, $pageSize");
		echo json_encode($rows);
		exit;
	}
	
	public function distributionDetailsAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$result = $db->fetchRow(
			"SELECT * FROM uuch_shipFeeConfig WHERE id = :id",
			array('id' => $this->_getParam('id'))
		);
		echo json_encode($result);
		exit;
	}
	
	public function distributionEditAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$set = array (
			'cityid' => (int)$this->_getParam('cityid'),
			'shipTypeId' => (int)$this->_getParam('shipTypeId'),
			'agentCompanyId' => (int)$this->_getParam('agentCompanyId'),
			'discount' => $this->_getParam('discount'),
			'price' => $this->_getParam('price'),
			'weight' => $this->_getParam('weight'),
			'startweight' => $this->_getParam('startweight'),
			'startPrice' => $this->_getParam('startPrice'),
			'queryurl' => $this->_getParam('queryurl'),
			'oil' => $this->_getParam('oil'),
			'clearancePrice' => $this->_getParam('clearancePrice'),
			'pushTime' => $this->_getParam('pushTime'),
			'state' => $this->_getParam('state'),
		);
		$where = $db->quoteInto('id = ?', $this->_getParam('id'));
		$rows_affected = $db->update('uuch_shipFeeConfig', $set, $where);
		echo 'editok';
		exit;
	}
	
	public function distributionAddAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$row = array (
			'cityid' => (int)$this->_getParam('cityid'),
			'shipTypeId' => (int)$this->_getParam('shipTypeId'),
			'agentCompanyId' => (int)$this->_getParam('agentCompanyId'),
			'discount' => $this->_getParam('discount'),
			'price' => $this->_getParam('price'),
			'weight' => $this->_getParam('weight'),
			'startweight' => $this->_getParam('startweight'),
			'startPrice' => $this->_getParam('startPrice'),
			'queryurl' => $this->_getParam('queryurl'),
			'oil' => $this->_getParam('oil'),
			'clearancePrice' => $this->_getParam('clearancePrice'),
			'pushTime' => $this->_getParam('pushTime'),
			'state' => $this->_getParam('state'),
		);
		$rows_affected = $db->insert('uuch_shipFeeConfig', $row);
		echo 'addok';
		exit;
	}
	
	public function distributionDelAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->delete('uuch_shipFeeConfig', $where);
		echo json_encode(true);
		exit;
	}
	
	public function distributionEnableAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_shipFeeConfig', array('state' => 1), $where);
		echo json_encode(true);
		exit;
	}
	
	public function distributionDisableAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_shipFeeConfig', array('state' => 0), $where);
		echo json_encode(true);
		exit;
	}
    
}